/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Student;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;
import org.xml.sax.SAXException;

/**
 *
 * @author aptech
 */
public class StudentTreeWalk {

    public void twReadAll() {
        try {
            Document doc = null;
            try {
                doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("student.xml");
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
            }
            DocumentTraversal traversal = (DocumentTraversal) doc;
            TreeWalker treewalk = traversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
//            traverseNode(treewalk, "");
            traverseNode(treewalk, "");
        } catch (SAXException ex) {
            Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    void traverseNode(TreeWalker treewalk, String indent) {
        Node node = treewalk.getCurrentNode();
//        String s = treewalk.firstChild().getNodeName();
//        s = treewalk.firstChild().getNodeName();
        String tail =
                (node.getNodeName().equals("code")
                || node.getNodeName().equals("name")
                || node.getNodeName().equals("email"))
                ? " : " + node.getTextContent() : "";
        System.out.println(indent + "" + ((Element) node).getTagName() + tail);
        for (Node n = treewalk.firstChild(); n != null; n = treewalk.nextSibling()) {
            traverseNode(treewalk, indent + "\t");
        }
        treewalk.setCurrentNode(node);
    }

    public ArrayList<Student> readdAllTreeWalk() {
        ArrayList<Student> listS = new ArrayList<Student>();
        try {
            Document doc = null;
            try {
                doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("student.xml");
            } catch (SAXException ex) {
                Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
            }
            DocumentTraversal traversal = (DocumentTraversal) doc;
            TreeWalker treewalk = traversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
            Node n = null;
            Student st = null;
            for (n = treewalk.firstChild(); n != null; n = treewalk.nextSibling()) {
                if (n.getNodeName().equals("student")) {
//                    if(st != null){
//                        listS.add(st);
//                    }
                    st = new Student();
//                    String s = treewalk.getRoot().getNodeName().toString();
////                    id = Integer.valueOf(n.getAttributes().item(0).getTextContent());
//                    for (Node n1 = n.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
//                        s = n1.getNodeName();
//                        if (n1.getNodeName().equals("name")) {
//                            st.setName(n1.getTextContent());
//                        } else {
//                            if (n1.getNodeName().equals("email")) {
//                                st.setEmail(n1.getTextContent());
//                            }
//                        }
//                        listS.add(st);
//                    }
                }
                if (n.getNodeName().equals("name")) {
                    st.setName(n.getTextContent());
                } else {
                    if (n.getNodeName().equals("email")) {
                        st.setEmail(n.getTextContent());
                        listS.add(st);
                    }
                }
            }
            return listS;
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(StudentTreeWalk.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    //void readStep(TreeWalker treewalk, ArrayList<Student> list, ){}
    public static void main(String[] args) {
        StudentTreeWalk tw = new StudentTreeWalk();
//        tw.twReadAll();
        tw.readdAllTreeWalk();
    }
}
